import WebStorageCache from 'web-storage-cache'

type CacheType = 'localStorage' | 'sessionStorage'

export const CACHE_KEY = {
  // 用户相关
  USER: 'user', // 用户信息
  ROLE_ROUTES: 'roleRoutes', //用户菜单信息
  // 系统设置
  IS_DARK: 'isDark',
  LANG: 'lang',
  THEME: 'theme',
  LAYOUT: 'layout',
  DICT_CACHE: 'dictCache',
  // 登录
  LoginForm: 'loginForm',
  TenantId: 'tenantId',
  LastRequest: 'lastRequest'
}

const localWsCache = new WebStorageCache({ storage: 'localStorage' })
const sessionWsCache = new WebStorageCache({ storage: 'sessionStorage' })

export const useCache = (type: CacheType = 'localStorage') => {
  const wsCache = type === 'localStorage' ? localWsCache : sessionWsCache
  return { wsCache }
}

export const deleteUserCache = () => {
  const { wsCache } = useCache()
  wsCache.delete(CACHE_KEY.USER)
  wsCache.delete(CACHE_KEY.ROLE_ROUTES)
  // 不要清理LoginForm缓存
}
